refactored solution

removed unused variables and functions
grouped code into self calling closures to prevent pollution of global
namespace