changed control flow
declared new functions.
added new functions
corrected error