Improved input type inference, bug fixing, code simplification

The input type inference has been improved to support also complicated use cases. It is now far more robust. I also tried to simplify the code a little bit by introducing helper functions.