Fixing issues
Restructured the code a bit more to make it flexible