Removed some more code duplication. Issue #4.
Fixed formatting.