new try to detect mobile devices and tidy up code