fix bug with incorrect index
fix bugs + refactor
fix bugs + add functionality