Cleaned up the file and reimplemented a few more parts.