Refactored, added more validation