created functions and improved flow