rewritten, maybe tag & dep logic works, maybe bugs fixed