Improved indexing and some bug fixes