Lots of improvements and bug fixing