cleaned up code and added more helper functions