refactored most functions into lib
added index to version field ... really need index on version field in data array somehow ...