Moved replicated code into a function and fixed a bug with eval not working on '\n\r'