refactor lab 3 part B, more stable version