Refactor most things for viewer, remove ugly code