Refactor

Restructuring to make it simpler and more flexible