refactored : renaming vars and reordering functions.