added check for function NameExist and cleaned code