Added Authentication and made further changes to improve code