Various logic fixes and some features added.