code refactor (still some features missing?)