Major refactor

Fix all known bugs