refactored with several new ideas
t