improved function detection; few bug fixes