refactored by encapsulating nodes code using classes