fixed math functions to work on device