Added postgresql support
Code cleanup