Handling last line without newline character at the end
Removed code duplication; releasing buffer after usage
Removed debug output