Added a few checks and corrections