Rough reimplementation that is extremely buggy