Refactored and added some additional functionality