Use ast.literal_eval() instead of eval()
Small refactoring and style changes, inspired by blackwithwhite666's fork