Fix undefined error and format code

Moved some variable defines around to fix undefined errors.
Formatted code to follow editorconfig.

This should fix #18