Refactored and added some functions.