redesign of code finished, seems to work