(cherry picked from commit 92ea7198042c9946f0e106f34ebfd27da1e75f93)
Trying to keep platform dependency on the headers and clean the main code a little bit.