overhaul and refactor of the code