actual working code although i expect to find more bugs lol