Moved type inference to before backend operations. Fixed a few bugs.