Refactor and fix bugs

I don't think that the basic approach should be to let a trailing new
line, so I've just removed that.