Cleanup, reformat, and Mavenize

Makes it easier to build, removed some redundant casts, use foreach to simplify code.