Refactored code to be a little cleaner, removed duplicate code