some refactor, change var names. hope i didn't break