Moved code out of index into separate lib/ files