Fixed errors and refactored