bugfix

fixed minor bugs
replaced some loops with functions of constant complexity
cleaned workspace