Added unique constraint and fixed indexes/stack traces