Updated script for new API, fixed bugs