Quite a lot of changes made. Cleaner code. A few more specs added.