Refactored code and added a few time related functions