Added some more simplification, fixed function type bug