Code fixes and new features

Code is still a work in progress. Do not use unless you know what you
are doing.