refactor and make it work