added new tests, improved function