functions fully tested with lots of bug fixes