Refactored code and corrected errors
Re-factored again