Fixed a number of bugs and added derivation