I trieed my best to debug, failed. Will likely rewrite this