Fix for issue #15

Moved var declarations to top of functions for backwards compatibility
Corrected typos