made it work cross platform, added additional checks