fixed bug, trimmed code