Improved input type inference, bug fixing, code simplification

This closes #176