Rewritten frontend.py. It is now much cleaner.