Refactored function parsing

Fixed bugs in parsing