modified for cleaner output (easier to test)
more consistent whitespace in js functions. remove unnecessary variables
fix unit tests for modified functions, implement missing tests