Reindented, fixed call to lists delete.
Removed io_lib:format calls and used iolists instead, reorderd some interla code to be at the end of the file. Used list concatination instead of flatten.