Cleaned up a bit and added Apache 2 License.

Please review the changes and merge if ok!