Some functions rewerited, bug fixes