Some refactoring, added support of while and for loops and functions.