Cleaned up and made the functions more readable