pep8 cleaning
Moved compiled regex at the top
Do not re-open the file to write into it
Use flag switching instead of place-holder variables
Revert "Do not re-open the file to write into it"

This reverts commit 97ec29d7b3a2d29f7700cd94575323cd7f7cbd3f.
Fixed faulty return statemnents
Fixed badly increased version