Move cursor logic out of connection + fix + make cursor jsonifiable