refactored whole form logic