Added basic derivative functionality

also cleaned stuff up a tiny bit