Fixes #19. Also changed some buffer handling to prevent garbage text being sent.