added Math.sign function for more clarity