This version is not perfect and needs refactor